	<?php
//订单管理基本方法
//管理员、企业必须继承此类实现私有业务

abstract class module_order{
	
	//构造方法，接收url参数进行分发
	function __construct(){
		global $tp;
		global $act;
		global $company;

		$this->$act();
		$tp->assign('nav','订单管理');
		$tp->assign('is_admin',$company['is_admin']);
	}
	
	//订单列表
	protected function index(){
		global $tp;
		global $Db;
		global $Base;
		global $company;

		$size = 15;
		$page = intval($_REQUEST['page']);
		$page = $page<1 ? 1 : $page;
		$c_id = intval($_REQUEST['c_id']);
		$order_status = isset($_REQUEST['order_status']) ? intval($_REQUEST['order_status']) : -1;
		
		//如果是商家，强制赋值$c_id
		if(!$company['is_admin'])	$c_id = $company['p_id']? $company['p_id']: $company['id'];
	
		$orders = $this->get_order_list($c_id,$order_status,$page,$size);
		//print_r($orders);
		
		$tp->assign('orders',$orders);
	
		$pages = Common::set_page($size,$page,$orders['count']);
		foreach ($pages as $key => $value) {
			 $pages[$key]['url'] = "/".COMPANY_PATH."/?m=order&page=".$value['num'].$orders['spm'];
		}
		$tp->assign("page",$page);
		$tp->assign("size",$size);
		$tp->assign("pages",$pages);
	}

	//订单详情
	protected function detail(){
		global $tp;
		global $Db;
		global $Base;
		global $company;
		global $Json;
		global $_lang;

		$id = intval($_REQUEST['id']);

		$sql = "SELECT * FROM ".$Base->table('company_order')." WHERE id=$id ";
		
		//如果是商家，强制赋值$c_id
		if(!$company['is_admin'])	$sql .= "and c_id=".($company['p_id'] ? $company['p_id'] : $company['id']);
		
		$order = $Db->query($sql,"Row");
		
		//$order不存在
		if(!$order){
			Common::base_header("Location:".$_lang['host']."company/admin/?m=order\n");
		}

		$sql = "SELECT i.name,i.price,i.num,i.remark FROM ".$Base->table('company_order_products')." as i ".
			   "WHERE i.oid = $id ORDER BY i.id";
		$order['product']= $Db->query($sql);
		//print_r($order);
		
		//显示数据
		if(empty($_POST)){
			$tp->assign('order',$order);
		}
		//修改订单状态
		else{
			$res['status'] = 0;
			$data = array('order_status'=>intval($_POST['order_status']));
			
			if($data['order_status']==3 && $order['order_status']!=2){
				$res['msg'] = '只有已支付的订单才能完成！';
			}
			else{
				if($data['order_status']==2){
					$data['paytime'] = date('Y-m-d H:i:s',Common::gmtime());
				}
				$Db->update($Base->table('company_order'),$data,array('id'=>$id));
				$res=array('status'=>1,'msg'=>'提交成功','href'=>'/'.COMPANY_PATH.'/?m=order');
			}
			echo $Json->encode($res);
			exit;
		}	
	}
	
	//删除订单
	protected function delete(){
		global $tp,$Db,$Base,$Json,$company;
		$oid = intval($_POST['oid']);
		$res['status'] = 0;
		//检查订单是否存在
		$sql = "select id,order_status from ".$Base->table('company_order')." where id=$oid ";
		if(!$company['is_admin']){
			$sql .= "and c_id=".($company['p_id'] ? $company['p_id'] : $company['id']);
		}
		$order = $Db->query($sql,'Row');
		if(!$order){
			$res['msg'] = '订单ID不存在或不合法！';
		}
		else if($order['order_status']==2 || $order['order_status']==3){
			$res['msg'] = '已支付或已完成的订单不能删除！';
		}
		else{
			//删除订单
			$sql = "delete from ".$Base->table('company_order')." where id=$oid";
			$Db->execSql($sql);
			//删除订单商品
			$sql = "delete from ".$Base->table('company_order_products')." where oid=$oid";
			$Db->execSql($sql);
			$res = array('status'=>1);
		}
		echo $Json->encode($res);
		exit;
	}
	
	//获取订单列表
	private function get_order_list($c_id,$order_status,$page,$size){
		$spm = '';
		$sql = "SELECT o.*,c.name as cname FROM ".$GLOBALS['Base']->table('company_order')." as o ".
			   "LEFT JOIN ".$GLOBALS['Base']->table('company')." as c ON o.c_id=c.id ".
			   "WHERE 1=1 ";

		//按商家显示
		if($c_id>0){
			$sql .= "AND c.id=$c_id ";
			$spm .= "&c_id=$c_id";
			$GLOBALS['tp']->assign('c_id',$c_id);
		}
		//按订单状态显示
		if($order_status>=0){
			$sql .= "AND o.order_status=$order_status ";
			$spm .= "&order_status=$order_status";
			$GLOBALS['tp']->assign('order_status',$order_status);
		}
		
		//echo $sql;
		$rescount = count($GLOBALS['Db']->query($sql));
		$sql .= "ORDER BY id DESC  LIMIT ".($page-1)*$size.", ".$size."";
		$list = $GLOBALS['Db']->query($sql);
		return array('list'=>$list,'count'=>$rescount,'spm'=>$spm);
	}
}

?>